using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;

namespace Workflows.Components
{
    /// <summary></summary>
    public partial class GlobalVariableGetterEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private Dictionary<string, GlobalVariable> _GlobalVariables;
        private GlobalVariableGetterComponent _GetterComponent;

        /// <summary></summary>
        public GlobalVariableGetterEditor()
        {
            InitializeComponent();
        }

        private void GlobalVariableGetterEditor_Load(object sender, EventArgs e)
        {
            this._GlobalVariables = new Dictionary<string, GlobalVariable>();
            this._GetterComponent=new GlobalVariableGetterComponent();
        }

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(GlobalVariableGetterComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            try
            {
                this.cbo_GlobalVariableNames.Items.Clear();
                this.cbo_GlobalVariableNames.SelectedIndex = -1;
                this.cbo_GlobalVariableNames.Text = string.Empty;
                this._GlobalVariables = new Dictionary<string, GlobalVariable>();
                if (globalVars != null && globalVars.Length > 0)
                {
                    foreach (GlobalVariable globalVar in globalVars)
                    {
                        this._GlobalVariables.Add(globalVar.VarName, globalVar);
                        this.cbo_GlobalVariableNames.Items.Add(globalVar.VarName);
                    }
                }
                this._OwnerActivityNode = activityNode;
                this._GetterComponent = (GlobalVariableGetterComponent) wfComponent;


                this.dv_Bindings.Rows.Clear();
                if (this._GetterComponent.GlobalVariableNames != null &&
                    this._GetterComponent.GlobalVariableNames.Count > 0)
                {
                    foreach (string gVarName in this._GetterComponent.GlobalVariableNames)
                    {
                        DataGridViewRow dr = new DataGridViewRow();
                        DataGridViewTextBoxCell cellGVarName = new DataGridViewTextBoxCell();
                        cellGVarName.Value = gVarName;
                        dr.Cells.Add(cellGVarName);
                        this.dv_Bindings.Rows.Add(dr);
                    }
                }
                this.dv_Bindings.Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btn_AddBinding_Click(object sender, EventArgs e)
        {
            if(this.cbo_GlobalVariableNames.SelectedIndex>=0)
            {
                string varName = this.cbo_GlobalVariableNames.SelectedItem.ToString();
                DataGridViewRow dr = new DataGridViewRow();
                DataGridViewTextBoxCell cellGVarName = new DataGridViewTextBoxCell();
                cellGVarName.Value = varName;
                dr.Cells.Add(cellGVarName);
                this.dv_Bindings.Rows.Add(dr);
            }
        }

        private void cbo_GlobalVariableNames_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.cbo_GlobalVariableNames.SelectedIndex >= 0)
            {
                string globalVarName = this.cbo_GlobalVariableNames.SelectedItem.ToString();
                if (this._GlobalVariables.ContainsKey(globalVarName))
                {
                    GlobalVariable globalVar = this._GlobalVariables[globalVarName];
                    this.lbl_GlobalVarDataType.Text = globalVar.DataType.ToString();
                }
            }
        }

        public TestResult Test()
        {
            return new TestResult(true, string.Empty);
        }

        public void SaveSettings()
        {
            List<string> varNames=new List<string>();
            List<GlobalVariable> gVars=new List<GlobalVariable>();
            foreach(DataGridViewRow dr in this.dv_Bindings.Rows)
            {
                if(!dr.IsNewRow && dr.Cells[0].Value !=null)
                {
                    string varName = dr.Cells[0].Value.ToString();
                    if(!varNames.Contains(varName))
                    {
                        varNames.Add(varName);
                        gVars.Add(this._GlobalVariables[varName]);
                    }
                }
            }

            if(varNames.Count>0)
            {
                this._GetterComponent.GlobalVariableNames = varNames;
                this._GetterComponent.Output =
                    new GlobalVariableGetterOutput(this._GetterComponent.ComponentID, Guid.Empty, gVars);
            }
            else
            {
                this._GetterComponent.GlobalVariableNames = null;
                this._GetterComponent.Output =
                    new GlobalVariableGetterOutput(this._GetterComponent.ComponentID, Guid.Empty, null);
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();

            if (this.UpdateComponentSettingEvent != null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._GetterComponent);
            }

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}